//
//  WMpingLunCell.swift
//  GZWeibo007
//
//  Created by 汪明 on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class WMpingLunCell: UITableViewCell {

   
    
    
    var comment: CZComments? {
        
        didSet{
            
            topView.comment = comment
    
            if let text = comment?.text {
            
                if text.characters.count > 1 {
                
                
                    
                    let nsmutableString = NSMutableString(string: text)
                    let atttext = NSMutableAttributedString(string: text)
                    let x = nsmutableString.rangeOfString("@")
                    
                    if x.length != 0 {
                        
                        let ss1 = nsmutableString.substringFromIndex(x.location + 1)
                        let nsmutableString1 = NSMutableString(string: ss1)
                        let x1 = nsmutableString1.rangeOfString(":")
                        
                        if x1.length == 0 && nsmutableString1.length < 20 && x.location == 0{
                        
                           
                        atttext.setAttributes([NSForegroundColorAttributeName : UIColor.blueColor()], range: NSRange.init(location: x.location, length: text.characters.count ))
                           
                     
                        }else if x1.length != 0 {
                        
                       
                          let range = NSRange.init(location: x.location, length: x1.location + 1) ///
                         
                            
                            atttext.setAttributes([NSForegroundColorAttributeName : UIColor.blueColor()], range: range)
                            contentLabel.attributedText = atttext

                        }
                        
                        
                        
                    }
                    
                    contentLabel.attributedText = atttext


                
                }
                
            }
            
        }
        
        
    }
    
    func preapreUI(){
    
      
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        
        
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44))
        
        topView.separatorView.backgroundColor = UIColor(white: 1, alpha: 1)
        
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 52, y: 20))
        
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 44))
 
    }
    
       override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 准备UI
        preapreUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    ///设置cell的大小
    func rowHeight(comment: CZComments) -> CGFloat {
        // 设置cell的status,cell会根据status里面的内容重新设置每个控件的内容
        self.comment = comment
        
        // 更新布局,系统会根据控件的内容和约束重新布局每个控件位置和大小
        layoutIfNeeded()
        
        // 获取底部视图的最大的Y值
        let maxY = CGRectGetMaxY(contentLabel.frame)
        return maxY
    }
    

    
    
    //MARK: 懒加载
    private lazy var topView:CZTopView = CZTopView()
    
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        
        // 字体大小
        label.font = UIFont.systemFontOfSize(14)
        
        // 字体颜色
        label.textColor = UIColor.darkGrayColor()
        
        // 换行
        label.numberOfLines = 0
        
        // 设置折行文本宽度(换行的宽度)
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * 8
        
        return label
    }()


    

}
